//
//  IdeaBaseVC.m
//  planner
//
//  Created by 함동균 on 2014. 3. 14..
//  Copyright (c) 2014년 idea. All rights reserved.
//

#import "IdeaBaseVC.h"

@interface IdeaBaseViewController ()
@property (nonatomic, strong) UIColor *backgroundColor;
@end

@implementation IdeaBaseViewController

// Lazy instanciators
- (NSString *)tabBarName
{
    if (!_tabBarName) {
        _tabBarName = TAB_BAR_CALENDAR;
    }
    return _tabBarName;
}

// Property fetcher from theme
- (UIColor *)getColorForProperty:(NSString *)property
{
    UIColor *color;
    color = [Theme colorProperty:property fromTabMenu:self.tabBarName];
    return color;
}

// Update tab bar tint color and navigation bar background color when view is loaded
- (void)viewDidAppear:(BOOL)animated
{
    [self updateTheme];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)updateTheme
{
    [self.tabBarController.tabBar setTintColor:[self getColorForProperty:BACKGROUND_COLOR]];
    [self.navigationController.navigationBar setBarTintColor:[self getColorForProperty:BACKGROUND_COLOR]];
    [self.navigationController.navigationBar setTranslucent:NO];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [self getColorForProperty:NAVIGATION_TITLE_TEXT_COLOR]}];
    
    for (UIBarButtonItem *item in self.navigationItem.leftBarButtonItems) {
        [item setTitleTextAttributes:@{NSForegroundColorAttributeName: [self getColorForProperty:NAVIGATION_ITEM_TEXT_COLOR]} forState:UIControlStateNormal];
        [item setTintColor:[self getColorForProperty:NAVIGATION_ITEM_TEXT_COLOR]];
    }
    
    for (UIBarButtonItem *item in self.navigationItem.rightBarButtonItems) {
        [item setTitleTextAttributes:@{NSForegroundColorAttributeName: [self getColorForProperty:NAVIGATION_ITEM_TEXT_COLOR]} forState:UIControlStateNormal];
        [item setTintColor:[self getColorForProperty:NAVIGATION_ITEM_TEXT_COLOR]];
    }
}

- (NSManagedObjectContext *)getManagedObjectContext
{
    IdeaAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = nil;
    context = [delegate managedObjectContext];
    return context;
}
@end
